<template>
	<view class="content">
		<!-- 搜索框 -->
		<view class="cu-bar bg-green search fixed" :style="[{top:0 + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input type="text" placeholder="请输入关键字" confirm-type="search" @input="searchCustom"></input>
			</view>
		</view>
		<mescroll-uni class="my-mescroll" ref="mescrollRef" @init="mescrollInit" :down="downOption" :up="upOption" @down="downCallback" @up="upCallback">
		
		<view class="inner-box">
			<view class="list-cont">
				<checkbox-group class="block" @change="CheckboxChange">
					<view class="item" v-for="(item, index) in dataList">
						<checkbox class='margin' :class="item.checked==true?'checked':''" :checked="item.checked==true?true:false" :value="item.productId"></checkbox>
						<view class="name">{{item.name}}</view>
					</view>
					 
				</checkbox-group>
			</view>
		</view>
		</mescroll-uni>
		<!-- <view class="footer-box">
			<view class="btn">保存</view>
		</view> -->
	</view>
</template>

<script>
	import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';
	import api from '@/api/api.js';
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				mescroll:undefined,
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption: {
					onScroll:true,
					use: true, // 是否启用上拉加载; 默认true
					auto: true, // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 1, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				dataList: [],
				searchContent:'',
				
			}
		},
		onLoad() {			
			// 修改顶部导航背景色
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: '#39b54a',
				animation: {
					duration: 400,
					timingFunc: 'easeIn'
				}
			})
		},
		methods: {
			// mescroll组件初始化的回调,可获取到mescroll对象
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				this.getProductList(page);
			},
			getProductList(page){
				console.log(page.num)
				var data = {page:page.num,searchVal:this.searchContent};
				var that=this;
				api.getProductList(data).then(
					res => {
						if(res.code==200){
							//设置列表数据
							if (page.num ==1) {
								that.dataList = res.data.list; 
								
							} else {
								that.dataList = that.dataList.concat(res.data.list);
								 
							}
							that.mescroll.endBySize(res.data.list.length, res.data.total);
							
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
							that.dataList = null;
							mescroll.endErr();
							// that.mescroll.endSuccess(0);
						}
					},
					rej => {}
				);
			},
			// 顶部搜索
			searchCustom(e) {
				let key = e.detail.value
				this.searchContent=key;
				this.upCallback({num:1,size:10})
			},
			CheckboxChange(e) {
				 
				var items = this.dataList,
					values = e.detail.value;
				var data=[];
				for (var i = 0;i< values.length;  i++) {
					for (var j = 0; j< items.length; j++) {
						if (items[j].productId.toString() == values[i]) {
							items[j].checked = true;
							data.push(items[j]);
						}
					}
				}	
				//更新上一个界面数据
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2]; //上一个页面
				prevPage.setData({
					 userdata: {
						'type':'product',
					   'item':data
					 }
				})
			},
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
	}
</style>
<style scoped lang="scss">
	.content{
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding-top: 20upx;
		.inner-box{
			height: calc(100% - 240upx);
			background: #fff;
			overflow: auto;
			margin-top: 100upx;
			.list-cont{
				padding: 0 24upx;
				background: #fff;
				.item{
					display: flex;
					align-items: center;
					&:last-child{
						.name{
							border-bottom: none;
						}
					}
					.margin{
						flex-shrink: 0;
						margin-right: 20upx;
					}
					.name{
						flex: 1;
						padding: 20upx 0;
						border-bottom: 1px solid #eee;
						font-size: 32upx;
						color: #333;
					}
				}
			}
		}
		.footer-box{
			padding: 20upx 24upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.btn{
				width: 100%;
				height: 80upx;
				line-height: 80upx;
				text-align: center;
				font-size: 28upx;
				border-radius: 8upx;
				background: #39b54a;
				color: #fff;
			}
		}
	}
</style>
